//
//  HJAssetPreviewCell.m
//  KaiChat
//
//  Created by lx on 2023/10/30.
//

#import "HJAssetPreviewCell.h"

@implementation HJAssetPreviewCell

- (void)didSingleTap:(UITapGestureRecognizer *)tap {}
- (void)didDoubleTap:(UITapGestureRecognizer *)tap {}
- (void)showControllView:(BOOL)show animation:(BOOL)animation {}
- (void)stop {}

@end


@implementation HJPhotoPreviewCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        _photoPreviewView = [[HJPhotoPreviewView alloc] initWithFrame:CGRectMake(8, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        [self.contentView addSubview:_photoPreviewView];
        self.contentView.backgroundColor = UIColor.clearColor;
    }
    return self;
}

 
- (void)didSingleTap:(UITapGestureRecognizer *)tap {
    
}

- (void)didDoubleTap:(UITapGestureRecognizer *)tap {
    
    if (!self.item.finished) {
        return;
    }
    
    if (self.photoPreviewView.zoomScale > 1) {
        [self.photoPreviewView setZoomScale:1 animated:YES];
    } else {
        CGPoint location = [tap locationInView:self.photoPreviewView];
        CGFloat maxZoomScale = self.photoPreviewView.maximumZoomScale;
        CGFloat width = self.photoPreviewView.bounds.size.width / maxZoomScale;
        CGFloat height = self.photoPreviewView.bounds.size.height / maxZoomScale;
        [self.photoPreviewView zoomToRect:CGRectMake(location.x - width/2, location.y - height/2, width, height) animated:YES];
    }
}

- (void)setItem:(HJPhotoItem *)item {
    [super setItem:item];
    
    _photoPreviewView.item = item;
}

- (UIView *)animationView {
    return _photoPreviewView.imageView;
}

- (UIView *)containView {
    return _photoPreviewView;
}

- (void)stop {
    
}

- (void)showControllView:(BOOL)show animation:(BOOL)animation {
    
    void (^animations)(void) = ^(void) {
        if (show) {
//            if (!self.item.finished) {
//                [self.photoPreviewView.progressLayer startSpin];
//            }
        } else {
            self.photoPreviewView.progressLayer.hidden = YES;
        }
      };
    
    if (animation) {
        [UIView animateWithDuration:0.25 animations:animations];
    } else {
        animations();
    }
}

@end
